package com.uzai.console.web.user;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.enums.IdentityEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.user.usertype.*;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupIdDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupInfoDto;
import com.uzai.console.dto.user.usertypegroup.UserTypeGroupQuery;
import com.uzai.console.entity.UserTypeGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.*;
import com.uzai.console.service.user.UserTypeService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.user.usertype.UserTypeInfoVo;
import com.uzai.console.vo.user.usertypegroup.UserTypeGroupInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月25日 20:15
 */
@Api(value = "会员类型",tags = "用户管理|会员类型")
@ApiSort(15)
@RestController
public class UserTypeContoller extends BaseController {
    private Logger logger = LoggerFactory.getLogger(UserTypeContoller.class);

    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private UserTypeGroupMapper userTypeGroupMapper;

    @ApiOperation(value = "分页查询会员类型列表", notes = "分页查询会员类型列表", response = UserTypeInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/usertype/findUserTypeList")
    public Object findUserTypeList(@RequestBody UserTypeQuery userTypeQuery){
        return userTypeService.findUserTypeList(userTypeQuery);
    }

    @ApiOperation(value = "查询会员类型详情", notes = "查询会员类型详情", response = UserTypeInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/usertype/findUserTypeInfo")
    public Object findUserTypeInfo(@RequestBody UserTypeIdDto userTypeIdDto){
        return userTypeService.findUserTypeInfo(userTypeIdDto);
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改支持跨身份升级", notes = "修改支持跨身份升级")
    @ApiOperationSupport(order = 3)
    @PostMapping("/usertype/updateUpIdentityEnum")
    public Object updateUpIdentityEnum(@RequestBody UserTypeUpdateUpIdentityDto userTypeUpdateUpIdentityEnumDto){
        userTypeService.updateUpIdentity(userTypeUpdateUpIdentityEnumDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改是否默认", notes = "修改是否默认")
    @ApiOperationSupport(order = 4)
    @PostMapping("/usertype/updateIsDefault")
    public Object updateIsDefault(@RequestBody UserTypeUpdateIsDefaultDto userTypeUpdateIsDefaultDto){
        userTypeService.updateIsDefault(userTypeUpdateIsDefaultDto);
        return true;
    }

    @ApiOperation(value = "添加会员类型前检验接口", notes = "添加会员类型前检验接口")
    @ApiOperationSupport(order = 5)
    @PostMapping("/usertype/addUserTypeUnit")
    public Object addUserTypeUnit(){
        UserTypeGroupQuery userTypeGroupQuery = new UserTypeGroupQuery();
        userTypeGroupQuery.setMerId(getLoginInfo().getMerId());
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
        if(userTypeGroupList == null || userTypeGroupList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先添加分组信息");
        }
        return true;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加会员类型", notes = "添加会员类型")
    @ApiOperationSupport(order = 5)
    @PostMapping("/usertype/addUserType")
    public Object addUserType(@RequestBody UserTypeInfoDto userTypeInfoDto){
        userTypeService.addUserType(userTypeInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改会员类型", notes = "修改会员类型")
    @ApiOperationSupport(order = 6)
    @PostMapping("/usertype/updateUserType")
    public Object updateUserType(@RequestBody UserTypeInfoDto userTypeInfoDto){
        userTypeService.updateUserType(userTypeInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除会员类型", notes = "删除会员类型")
    @ApiOperationSupport(order = 7)
    @PostMapping("/usertype/deleteUserType")
    public Object deleteUserType(@RequestBody UserTypeIdDto userTypeIdDto){
        userTypeService.deleteUserType(userTypeIdDto);
        return true;
    }

    @ApiOperation(value = "分页查询会员类型分组列表", notes = "分页查询会员类型分组列表", response = UserTypeGroupInfoVo.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/usertype/findUserTypeGroupList")
    public Object findUserTypeGroupList(@RequestBody UserTypeGroupQuery userTypeGroupQuery){
        return userTypeService.findUserTypeGroupList(userTypeGroupQuery);
    }


    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加会员类型分组", notes = "添加会员类型分组")
    @ApiOperationSupport(order = 9)
    @PostMapping("/usertype/addUserTypeGroup")
    public Object addUserTypeGroup(@RequestBody UserTypeGroupInfoDto userTypeGroupInfoDto){
        userTypeService.addUserTypeGroup(userTypeGroupInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除会员类型分组", notes = "删除会员类型分组")
    @ApiOperationSupport(order = 10)
    @PostMapping("/usertype/deleteUserTypeGroup")
    public Object deleteUserTypeGroup(@RequestBody UserTypeGroupIdDto userTypeGroupIdDto){
        userTypeService.deleteUserTypeGroup(userTypeGroupIdDto);
        return true;
    }

    @ApiOperation(value = "查询会员类型分组下拉列表框", notes = "查询会员类型分组下拉列表框", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 11)
    @PostMapping("/usertype/findUserTypeGroupSelect")
    public Object findUserTypeGroupSelect(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        UserTypeGroupQuery userTypeGroupQuery =  new UserTypeGroupQuery();
        userTypeGroupQuery.setMerId(getLoginInfo().getMerId());
        List<UserTypeGroup> userTypeGroupList = userTypeGroupMapper.findByList(userTypeGroupQuery);
        if(userTypeGroupList != null && userTypeGroupList.size() > 0){
            for(UserTypeGroup userTypeGroup : userTypeGroupList){
                SelectKeyVo selectKeyVo =  new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(userTypeGroup.getId()));
                //页面显示 身份-类型名称
                selectKeyVo.setValue(userTypeGroup.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @ApiOperation(value = "查询身份下拉列表框", notes = "查询身份下拉列表框", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 12)
    @PostMapping("/usertype/findIdentityEnumSelect")
    public Object findIdentityEnumSelect(){
        List<SelectKeyVo> selectKeyVoList = new ArrayList<>();
        IdentityEnum[] identitieArr = IdentityEnum.values();
        if(identitieArr != null && identitieArr.length > 0){
            for(IdentityEnum identity : identitieArr){
                SelectKeyVo selectKeyVo =  new SelectKeyVo();
                selectKeyVo.setKey(identity.getId().toString());
                selectKeyVo.setValue(identity.getDesc());
                selectKeyVoList.add(selectKeyVo);
            }
        }
        return selectKeyVoList;
    }

    @SystemLogType(model = SystemModelEnum.UserType, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "修改会员类型分组", notes = "修改会员类型分组")
    @ApiOperationSupport(order = 13)
    @PostMapping("/usertype/updateUserTypeGroup")
    public Object updateUserTypeGroup(@RequestBody UserTypeGroupInfoDto userTypeGroupInfoDto){
        userTypeService.updateUserTypeGroup(userTypeGroupInfoDto);
        return true;
    }


    @ApiOperation(value = "查询所有会员类型列表", notes = "查询所有会员类型列表", response = UserTypeInfoVo.class)
    @ApiOperationSupport(order = 14)
    @PostMapping("/usertype/findUserTypeListSelect")
    public Object findUserTypeListSelect(@RequestBody UserTypeQuery userTypeQuery){
        return userTypeService.findUserTypeListSelect(userTypeQuery);
    }

}
